import os
import re
import shutil
from collections import defaultdict
from datetime import datetime
from pprint import pprint

from openpyxl import load_workbook
from config import output_folder, teacher_name
from input import term
from parse import number_pattern
from teachCal import class_name_and_hour

result = defaultdict(list)

def get_num(s):
    m = number_pattern.search(s)
    if m:
        return m.group(1)
    return ''


def convert_data(total_lessons):
    for lesson in total_lessons:
        class_name = lesson[5]
        for item in lesson:
            m = re.search("(20.*班)", str(item))
            if m:
                teach_class_temp = m.group(1)
                teach_class_list = []
                if ';' in teach_class_temp:
                    teach_class_list = teach_class_temp.split(';')
                else:
                    teach_class_list.append(teach_class_temp)
                for teach_class in teach_class_list:
                    if class_name in result:
                        if teach_class not in result.get(class_name):
                            result.get(class_name).append(teach_class)
                    else:
                        result[class_name] = [teach_class]

def create_student_list(total_lessons, file='template/student_list_temp.xlsx'):
    convert_data(total_lessons)
    pprint(result)
    # key=课程名， data=教学班
    for key, data in result.items(): # 按课程分类，一个课程一个excel文件
        outfile = os.path.join(output_folder, f'{teacher_name}_点名册_{key}.xlsx')
        shutil.copy(file, outfile)
        workbook = load_workbook(outfile)
        class_hour = class_name_and_hour.get(key)
        for teach_class in data:
            src_sheet = f'student-{class_hour}'
            if not src_sheet in workbook.sheetnames:
                if int(class_hour) > 32:
                    src_sheet = 'student-32'
                elif 16 < int(class_hour) < 32:
                    src_sheet = 'student-32'
                elif int(class_hour) < 16:
                    src_sheet = 'student-16'
            sheet = workbook.copy_worksheet(workbook[src_sheet])
            sheet.title = teach_class
            sheet.cell(1,1).value = sheet.cell(1,1).value.replace('@term', term)
            sheet.cell(2,1).value = sheet.cell(2,1).value.replace('@class_name', teach_class).replace('@teacher_name',teacher_name).replace('@lesson_name', key)
            # 写入学生的学号、姓名
            student_list_file = os.path.join('template', 'student_list', teach_class + '.txt')
            if os.path.exists(student_list_file):
                row_no = 5
                with open(student_list_file, 'r') as f:
                    for line in f:
                        stu_id, stu_name = line.split()
                        sheet.cell(row_no, 2).value = stu_id
                        sheet.cell(row_no, 3).value = stu_name
                        row_no += 1

        # 删除模版sheet
        workbook.remove_sheet(workbook['student-16'])
        workbook.remove_sheet(workbook['student-32'])
        workbook.save(outfile)
        workbook.close()

